/**
 * @(#) MedicalMoneyProxy.as
 *
 * @date		2011. 03. 25
 * @version		1.0
 * @author		baek kwang hyoun
 * @description	건강보험증 Proxy - Proxy
 * 				건강보험증를 조회를한다.
 */
package com.skt.common_hr.beguide.model
{
	import com.skt.common_hr.beguide.model.vo.bankreqListVO;
	import com.skt.common_hr.beguide.model.vo.bankreqVO;
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.utils.ObjectUtil;
 
	public class BeguideProxy extends CommonProxy
	{
		public static const NAME:String = "BankreqProxy";

		public static const LOAD_MEDICALMONEY_LIST_COMPLETE:String	= "LoadMedicalMoneyListComplete";
		public static const LOAD_MEDICALMONEY_LIST_FAILED:String	= "LoadMedicalMoneyListFailed";


		public var mainList:ArrayCollection;
		public var historyList:ArrayCollection;
		
		public var bankList:ArrayCollection;
		public var statusList:ArrayCollection;
		
		
		public var subTabList:ArrayCollection;
		public var acOitab:ArrayCollection;

		public var key:String;

		//지원기준
		public var loadFaultMessage:String;






		/**
		 * 생성자
		 *
		 * @param
		 * @return void
		 */
		public function BeguideProxy(data:Object=null)
		{
			super(NAME, data);
		}

		// 메인리스트 호출
		public function loadList(params:bankreqVO, setFunc:Function, dv:String):void
		{
			//var params:Object = new Object();
			//params["I_PERNR"] = pernr;
			loadData("Z_HR_B001", params, loadListResult, listFault, setFunc, dv);
		}




		public function loadListResult(event:ResultEvent):void
		{
			if(retCode==0) {
				
				bankList	= event.result.T_BCODE;
				mainList 	= parseMainList(ObjectUtil.copy(event.result.OTAB) as ArrayCollection );
				historyList = parseMainList(ObjectUtil.copy(event.result.OTAB1) as ArrayCollection);
				
				/* 
				var obj:Object = {};
				obj.CODE = '';
				obj.TEXT = '전체';
				statusList	= event.result.T_CODE as ArrayCollection;
				statusList.addItemAt(obj, 0);
				 */
				var dv:String = event.result.rtnObj;
				event.result.func(mainList, historyList, bankList, dv)
			}
			else {
				Alert.show('loadListResult err: ' + retText);
			}
		}




		// 리스트 업데이트
 		public function detailList(selecteItem:Object, setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_BUKRS"] = selecteItem.BUKRS;
			params["I_PERNR"] = selecteItem.PERNR;
			params["I_REQDT"] = selecteItem.REQDT_JSON;
			params["I_REQRN"] = selecteItem.REQRN;

			loadData("Z_HR_0142", params, detailListResult, listFault, setFunc);
		}
		public function detailListResult(event:ResultEvent):void
		{
			if(retCode==0) {
				subTabList 	= parseMainList(event.result.O_ITAB);
				acOitab		= parseSubList(event.result.O_FAM);

				var obj:Object = {};
				obj.acOitab		= subTabList;
				obj.acOfam	 	= acOitab;

				event.result.func(obj)
			}
			else {
				Alert.show('detailListResult err: ' + retText);
			}
		}


		// 삭제
 		public function deleteList(selecteItem:Object, gubun:String, setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = Variables.myPernr;
			params["I_GUBUN"] = gubun;
			params["I_ACTIO"] = "DEL";
			
			var tables:Object = new Object();
			var table:Array = new Array();
			var obj:Object = {};
			obj.BEGDA = selecteItem.BEGDA_JSON;
			obj.ENDDA = selecteItem.ENDDA_JSON;
			obj.BANKL = selecteItem.BANKL;
			obj.BANKN = selecteItem.BANKN;
			
			table.push(obj);
			tables["ITAB"] = table;
			saveData("Z_HR_B003", params, tables, deleteListResult, listFault, setFunc);
		}
		public function deleteListResult(event:ResultEvent):void
		{
			if(retCode==0) {				
				event.result.func();
			}
			else {
				Alert.show('deleteListResult err: ' + retText);
			}
		}

		// 재발급 사유 코드
		public var acReReqRsnCode:ArrayCollection;
		public function getReReqRsnCode(setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_DNAME"] = "ZDWHYRE";
			loadData("Z_HR_0003", params, getReReqRsnCodeResult, listFault, setFunc);
		}
		public function getReReqRsnCodeResult(event:ResultEvent):void
		{
			if(retCode==0) {
				var obj:Object = {CODE:"0", TEXT:"재발급사유를 선택하세요"};
				acReReqRsnCode = event.result.ITAB;
				acReReqRsnCode.addItemAt(obj, 0);
				event.result.func(acReReqRsnCode);
			}
			else {
				Alert.show('getReReqRsnCodeResult err: ' + retText);
			}
		}


		// 저장
		public function save(params:Object, itab:Object, setFunc:Function):void
		{
			var tables:Object = new Object();
			var table:Array = new Array();
			table.push(itab);
			tables["ITAB"] = table;
			saveData("Z_HR_B003", params, tables, saveResult, listFault, setFunc);
		}
		
		public function saveResult(event:ResultEvent):void
		{
			if(retCode==0) {
				event.result.func();
			}
			else {
				Alert.show(retText);
			}
		}



		private function parseMainList(recArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();

			for each(var obj:Object in recArr){
				var retObj:bankreqListVO = new bankreqListVO();				
				for(var field:String in obj) {
					if(field == "SDATE" || field == "BEGDA" || field == "ENDDA") {
						retObj[field+'_DT'] = DateUtil.stringToDateTime(obj[field]);
						retObj[field+'_TX'] = DateUtil.toDefaultString(retObj[field+'_DT']);
						retObj[field+'_JSON'] = DateUtil.toBasicString(retObj[field+'_DT']);						
						retObj[field] = obj[field];
					}
					else {
						retObj[field] = obj[field];
					}
				}
				retArr.addItem(retObj);
			}
			return retArr;
		} 

		private function parseSubList(recArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
//
//			for each(var obj:Object in recArr){
//				//var retObj:MedicalSubTabVO = new MedicalSubTabVO();
//				var retObj:hthIcI_FAM_VO = new hthIcI_FAM_VO();
//				for(var field:String in obj) {
//					if(field == "REQDT" || field == "SENDT" || field == "IEDAT") {
//						retObj[field+'_DT'] = DateUtil.stringToDateTime(obj[field]);
//						retObj[field+'_TX'] = DateUtil.toDefaultString(retObj[field+'_DT']);
//						retObj[field+'_JSON'] = DateUtil.toBasicString(retObj[field+'_DT']);
//						retObj[field] = DateUtil.stringToDateTime(obj[field]);
//					}
//					else if(field == "BETRG") {
//						retObj[field] = Math.round(obj[field]*100);
//					}
//					else {
//						retObj[field] = obj[field];
//					}
//				}
//				retArr.addItem(retObj);
//			}
			return retArr;
		}

		private function toJsonType(vo:Object):Object {
			var retObj:Object = new Object;
			for(var field:String in vo) {
				if(vo[field] is Date) {
					retObj[field] = DateUtil.toBasicString(vo[field]);
				}
				else {
					retObj[field] = vo[field];
				}
				trace(retObj[field]);
			}
			return retObj;
		}

		private function listFault(event:FaultEvent):void
		{
			Alert.show('HthIcProxy.as listFault err: ' + retText);
		}

		// VO 상세 가족조회
		private function parseDetailFamilyAc(recArr:ArrayCollection):ArrayCollection//hthDetailFamilyVO
		{
			var retArr:ArrayCollection = new ArrayCollection();

//
//			for each(var obj:Object in recArr){
//				var retObj:hthDetailFamilyVO = new hthDetailFamilyVO();
//				for(var field:String in obj) {
//					if(field == "REQDT" || field == "SENDT" || field == "IEDAT") {
//						retObj[field+'_DT'] = DateUtil.stringToDateTime(obj[field]);
//						retObj[field+'_TX'] = DateUtil.toDefaultString(retObj[field+'_DT']);
//						retObj[field+'_JSON'] = DateUtil.toBasicString(retObj[field+'_DT']);
//						retObj[field] = DateUtil.stringToDateTime(obj[field]);
//					}
//					else if(field == "BETRG") {
//						retObj[field] = Math.round(obj[field]*100);
//					}
//					else {
//						retObj[field] = obj[field];
//					}
//				}
//				retArr.addItem(retObj);
//			}
			return retArr;
		}

		

	}
}